package com.neutech.code.dao.impl;

import com.neutech.code.constant.Constant;
import com.neutech.code.dao.BusinessDAO;
import com.neutech.code.entity.Business;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BusinessDAOImpl implements BusinessDAO {
    private QueryRunner queryRunner=new QueryRunner(Constant.dataSourse);
    public List<Business> selectAll(){
        List<Business> businessList=new ArrayList<>();
        try {
            queryRunner.execute("select * from business", new ResultSetHandler<Object>() {
                @Override
                public Object handle(ResultSet resultSet) throws SQLException {
                    while (resultSet.next()) {
                        Business business=new Business();
                        business.setBusinessId(resultSet.getInt("business_id"));
                        business.setPassword(resultSet.getString("password"));
                        business.setBusinessName(resultSet.getString("business_name"));
                        business.setBusinessAddress(resultSet.getString("business_address"));
                        business.setBusinessExplain(resultSet.getString("business_explain"));
                        business.setStartPrice(resultSet.getBigDecimal("start_price"));
                        business.setDeliveryPrice(resultSet.getBigDecimal("delivery_price"));
                         businessList.add(business);
                    }
                    return businessList;
                }
            });
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return businessList;

    }
       public void addBusiness(Business business) {
           String sql = "INSERT INTO business (business_id,password, business_name, business_address, business_explain, start_price, delivery_price) " +
                   "VALUES (?,?,?,?,?,?,?)";
           try {
               queryRunner.update(sql,
                       business.getBusinessId(),
                       business.getPassword(),
                       business.getBusinessName(),
                       business.getBusinessAddress(),
                       business.getBusinessExplain(),
                       business.getStartPrice(),
                       business.getDeliveryPrice()
               );
           } catch (SQLException e) {
               throw new RuntimeException("Error inserting business data", e);
           }
       }
       public void updateBusiness(Business business){
           String sql = "UPDATE business " +
                   "SET password = ?, business_name = ?, business_address = ?, business_explain = ?, " +
                   "start_price = ?, delivery_price = ? " +
                   "WHERE business_id = ?";
           try {

               queryRunner.update(sql,
                       business.getPassword(),
                       business.getBusinessName(),
                       business.getBusinessAddress(),
                       business.getBusinessExplain(),
                       business.getStartPrice(),
                       business.getDeliveryPrice(),
                       business.getBusinessId()
               );

               System.out.println("商家数据更新成功！");
           } catch (SQLException e) {
               throw new RuntimeException("Error updating business data", e);
           }
       }


public void deleteBusiness(int businessId) {
    String sql = "DELETE FROM business WHERE business_id = ?";
    try {
        queryRunner.update(sql, businessId);
        System.out.println("商家数据删除成功！");
    } catch (SQLException e) {
        throw new RuntimeException("Error deleting business data", e);
    }
}
}

